package com.ruoyi.activiti.service.impl;

import com.ruoyi.activiti.domain.ActDeploymentVo;
import com.ruoyi.activiti.domain.WorkFlowVo;
import com.ruoyi.activiti.service.WorkFlowService;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class WorkFlowServiceImpl implements WorkFlowService {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private ManagementService managementService;

    public List<ActDeploymentVo> deployment(WorkFlowVo workFlowVo){
        String deploymentName = workFlowVo.getDeploymentName();
        Integer page = (workFlowVo.getPage()-1)*workFlowVo.getLimit();
        Integer limit = workFlowVo.getLimit();
        List<Deployment> deploymentList = repositoryService.createDeploymentQuery()
                .deploymentName(deploymentName)
                .listPage(page, limit);

        ArrayList<ActDeploymentVo> deploymentVoList = new ArrayList<>();
        deploymentList.forEach((deployment)->{
            ActDeploymentVo deploymentVo = new ActDeploymentVo();
            BeanUtils.copyProperties(deployment,deploymentVo);
            deploymentVoList.add(deploymentVo);
        });

        return deploymentVoList;
    }

    /**
     * 批量删除
     * @param workFlowVo
     */
    @Override
    @Transactional
    public void deleteBatch(WorkFlowVo workFlowVo) throws Exception {
        String[] ids = workFlowVo.getIds();
        for (int i = 0; i < ids.length; i++) {
            try {
                repositoryService.deleteDeployment(ids[i]);
            }catch (Exception e){
                throw new Exception(e);
            }
        }
    }
}
